package com.javasm.config;

import com.javasm.security.MyUsernamePasswordFilter;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.crypto.password.NoOpPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter;

/**
 * @author: Lisa
 * @className: SecurityConfig
 * @description:
 * @date: 2023/10/21 22:02
 * @VERSION: 0.1
 * @SINCE: jdk11
 */
@Configuration
//@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {


    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http.authorizeRequests()
                .antMatchers("/sendMail").permitAll()
                .anyRequest().authenticated();

        http.formLogin().permitAll();

        http.addFilterAt(myUsernamePasswordFilter(), UsernamePasswordAuthenticationFilter.class);

        http.csrf().disable();
    }

    public MyUsernamePasswordFilter myUsernamePasswordFilter() throws Exception {
        MyUsernamePasswordFilter filter = new MyUsernamePasswordFilter();
        filter.setAuthenticationManager(authenticationManagerBean());
        return filter;
    }

    @Bean
    public PasswordEncoder passwordEncoder(){
        return NoOpPasswordEncoder.getInstance();
    }
}
